﻿namespace YidanSoft.Library.EditorUtility.Menus
{
    using System;
    using System.Drawing;
    using System.Drawing.Text;
    using System.Reflection;
    using System.Resources;
    using System.Windows.Forms;
    using YidanSoft.Library.EditorUtility.CommandBars;
    using YidanSoft.Library.EditorUtility.General;
    using YidanSoft.Library.EditorUtility.Win32;

    public class MenuItemEx : MenuItem
    {
        private static Color bgColor = group.bgColor;
        private static int BITMAP_SIZE = 0x10;
        private static Color borderColor = group.borderColor;
        private EventHandler clickHandler;
        private static Color darkSelectionColor = group.darkSelectionColor;
        private static bool doColorUpdate = false;
        private static ColorGroup group = ColorGroup.GetColorGroup();
        private Bitmap icon;
        private static int iconSize = (SystemInformation.SmallIconSize.Width + 5);
        private int imageIndex;
        private System.Windows.Forms.ImageList imageList;
        private static int itemHeight;
        protected static System.Windows.Forms.ImageList menuImages = new System.Windows.Forms.ImageList();
        private static Color selectionColor = group.selectionColor;
        private string shortcuttext;
        private static int STRIPE_WIDTH = (iconSize + 2);
        private static Color stripeColor = group.stripeColor;
        private static Color transparentColor = Color.FromArgb(0xc0, 0xc0, 0xc0);

        static MenuItemEx()
        {
            menuImages.ImageSize = new Size(BITMAP_SIZE, BITMAP_SIZE);
            Assembly assembly = Assembly.GetAssembly(System.Type.GetType("YidanSoft.Library.EditorUtility.Menus.MenuItemEx"));
            ResourceManager manager = new ResourceManager("YidanSoft.Library.EditorUtility.Resources.ImagesMenu", assembly);
            Bitmap bitmap = (Bitmap) manager.GetObject("Glyphs");
            bitmap.MakeTransparent(transparentColor);
            menuImages.Images.AddStrip(bitmap);
        }

        public MenuItemEx() : this(null, null)
        {
        }

        public MenuItemEx(string name, EventHandler handler) : base(name, handler)
        {
            this.shortcuttext = "";
            this.icon = null;
            this.clickHandler = null;
            this.imageList = null;
            this.imageIndex = -1;
            this.Initialize(null, Shortcut.None, handler, null, -1);
        }

        public MenuItemEx(string name, EventHandler handler, Shortcut shortcut) : this(name, handler)
        {
            this.Initialize(null, shortcut, handler, null, -1);
        }

        public MenuItemEx(string name, Bitmap icon, Shortcut shortcut, EventHandler handler) : this(name, handler)
        {
            this.Initialize(icon, shortcut, handler, null, -1);
        }

        public MenuItemEx(string name, System.Windows.Forms.ImageList imageList, int imageIndex, Shortcut shortcut, EventHandler handler) : this(name, handler)
        {
            this.Initialize(this.icon, shortcut, handler, imageList, imageIndex);
        }

        public static MenuItem CloneMenu(MenuItemEx currentItem)
        {
            MenuItemEx ex = new MenuItemEx(currentItem.Text, currentItem.Icon, currentItem.Shortcut, currentItem.ClickHandler);
            ex.Enabled = currentItem.Enabled;
            ex.Checked = currentItem.Checked;
            ex.RadioCheck = currentItem.RadioCheck;
            foreach (MenuItemEx ex2 in currentItem.MenuItems)
            {
                ex.MenuItems.Add(CloneMenu(ex2));
            }
            return ex;
        }

        private void DoUpdateMenuColors()
        {
            ColorGroup colorGroup = ColorGroup.GetColorGroup();
            bgColor = colorGroup.bgColor;
            stripeColor = colorGroup.stripeColor;
            selectionColor = colorGroup.selectionColor;
            borderColor = colorGroup.borderColor;
            darkSelectionColor = colorGroup.darkSelectionColor;
            doColorUpdate = false;
        }

        public void DrawBackground(Graphics g, Rectangle bounds, DrawItemState state, bool toplevel, bool hasicon, bool enabled)
        {
            bool flag = (state & DrawItemState.Selected) > DrawItemState.None;
            if (flag || ((state & DrawItemState.HotLight) > DrawItemState.None))
            {
                if (toplevel && flag)
                {
                    bounds.Inflate(-1, 0);
                    g.FillRectangle(new SolidBrush(stripeColor), bounds);
                    if (ColorUtil.UsingCustomColor)
                    {
                        GDIUtil.Draw3DRect(g, bounds, ColorUtil.VSNetBorderColor, ColorUtil.VSNetControlColor);
                    }
                    else
                    {
                        ControlPaint.DrawBorder3D(g, bounds.Left, bounds.Top, bounds.Width, bounds.Height, Border3DStyle.Flat, Border3DSide.Right | Border3DSide.Top | Border3DSide.Left);
                    }
                }
                else if (enabled)
                {
                    g.FillRectangle(new SolidBrush(selectionColor), bounds);
                    g.DrawRectangle(new Pen(borderColor), bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1);
                }
                else
                {
                    YidanSoft.Library.EditorUtility.Win32.RECT rc = new YidanSoft.Library.EditorUtility.Win32.RECT();
                    IntPtr handle = base.Parent.Handle;
                    uint index = (uint) base.Index;
                    bool flag2 = WindowsAPI.GetMenuItemRect(IntPtr.Zero, handle, index, ref rc);
                    Rectangle rectangle = new Rectangle(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
                    Point mousePosition = Control.MousePosition;
                    if (!rectangle.Contains(mousePosition))
                    {
                        g.FillRectangle(new SolidBrush(bgColor), bounds);
                        g.DrawRectangle(new Pen(borderColor), bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1);
                    }
                    else
                    {
                        g.FillRectangle(new SolidBrush(stripeColor), bounds);
                        bounds.X += STRIPE_WIDTH;
                        bounds.Width -= STRIPE_WIDTH;
                        g.FillRectangle(new SolidBrush(bgColor), bounds);
                    }
                }
            }
            else if (!toplevel)
            {
                g.FillRectangle(new SolidBrush(stripeColor), bounds);
                bounds.X += STRIPE_WIDTH;
                bounds.Width -= STRIPE_WIDTH;
                g.FillRectangle(new SolidBrush(bgColor), bounds);
            }
            else
            {
                g.FillRectangle(SystemBrushes.Control, bounds);
            }
        }

        private void DrawCheckedRectangle(Graphics g, Rectangle bounds)
        {
            int num = bounds.Top + ((itemHeight - BITMAP_SIZE) / 2);
            int num2 = bounds.Left + ((STRIPE_WIDTH - BITMAP_SIZE) / 2);
            g.FillRectangle(new SolidBrush(selectionColor), (int) (bounds.Left + 1), (int) (bounds.Top + 1), (int) (STRIPE_WIDTH - 3), (int) (bounds.Height - 3));
            g.DrawRectangle(new Pen(borderColor), (int) (bounds.Left + 1), (int) (bounds.Top + 1), (int) (STRIPE_WIDTH - 3), (int) (bounds.Height - 3));
        }

        public void DrawIcon(Graphics g, Image icon, Rectangle bounds, bool selected, bool enabled, bool isChecked)
        {
            Color transparentColor = Color.FromArgb(0, 0x80, 0x80);
            ((Bitmap) icon).MakeTransparent(transparentColor);
            int y = bounds.Top + ((itemHeight - BITMAP_SIZE) / 2);
            int x = bounds.Left + ((STRIPE_WIDTH - BITMAP_SIZE) / 2);
            if (enabled)
            {
                if (selected)
                {
                    if (isChecked)
                    {
                        this.DrawCheckedRectangle(g, bounds);
                        g.DrawImage(icon, x + 1, y);
                    }
                    else
                    {
                        ControlPaint.DrawImageDisabled(g, icon, x + 1, y, Color.Black);
                        g.DrawImage(icon, x, y - 1);
                    }
                }
                else
                {
                    if (isChecked)
                    {
                        this.DrawCheckedRectangle(g, bounds);
                    }
                    g.DrawImage(icon, x + 1, y);
                }
            }
            else
            {
                ControlPaint.DrawImageDisabled(g, icon, x + 1, y, SystemColors.HighlightText);
            }
        }

        public void DrawMenuGlyph(Graphics g, Rectangle bounds, bool selected, bool bCheckMark)
        {
            int y = bounds.Top + ((itemHeight - BITMAP_SIZE) / 2);
            int x = bounds.Left + ((STRIPE_WIDTH - BITMAP_SIZE) / 2);
            if (base.Enabled)
            {
                g.FillRectangle(new SolidBrush(selected ? darkSelectionColor : selectionColor), (int) (bounds.Left + 1), (int) (bounds.Top + 1), (int) (STRIPE_WIDTH - 3), (int) (bounds.Height - 3));
                menuImages.Draw(g, x, y, bCheckMark ? 0 : 1);
                g.DrawRectangle(new Pen(borderColor), (int) (bounds.Left + 1), (int) (bounds.Top + 1), (int) (STRIPE_WIDTH - 3), (int) (bounds.Height - 3));
            }
            else
            {
                int num3 = bCheckMark ? 0 : 1;
                ControlPaint.DrawImageDisabled(g, menuImages.Images[num3], x, y, Color.Black);
            }
        }

        public void DrawMenuText(Graphics g, Rectangle bounds, string text, string shortcut, bool enabled, bool toplevel, DrawItemState state)
        {
            StringFormat format = new StringFormat();
            format.HotkeyPrefix = HotkeyPrefix.Show;
            bool flag = false;
            bool flag2 = false;
            if (SystemColors.Control.ToArgb() == Color.FromArgb(0xff, 0, 0, 0).ToArgb())
            {
                flag = true;
            }
            if (SystemColors.Control.ToArgb() == Color.FromArgb(0xff, 0xff, 0xff, 0xff).ToArgb())
            {
                flag2 = true;
            }
            string str = text;
            if (toplevel)
            {
                int index = text.IndexOf("&");
                if (index != -1)
                {
                    text = text.Remove(index, 1);
                }
            }
            int width = (int) g.MeasureString(text, SystemInformation.MenuFont).Width;
            int num3 = toplevel ? (bounds.Left + ((bounds.Width - width) / 2)) : ((bounds.Left + iconSize) + 10);
            int num4 = 4;
            if (toplevel)
            {
                num4 = 2;
            }
            int num5 = bounds.Top + num4;
            Brush brush = null;
            if (!enabled)
            {
                brush = new SolidBrush(Color.FromArgb(120, SystemColors.MenuText));
            }
            else if (flag)
            {
                brush = new SolidBrush(Color.FromArgb(0xff, SystemColors.MenuText));
            }
            else
            {
                brush = new SolidBrush(Color.Black);
            }
            if (flag2 && (((state & DrawItemState.HotLight) > DrawItemState.None) || (((state & DrawItemState.Selected) > DrawItemState.None) && !toplevel)))
            {
                brush = new SolidBrush(Color.FromArgb(0xff, Color.White));
            }
            if (toplevel)
            {
                text = str;
            }
            g.DrawString(text, SystemInformation.MenuFont, brush, (float) num3, (float) num5, format);
            if (!toplevel)
            {
                format.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
                g.DrawString(shortcut, SystemInformation.MenuFont, brush, (float) (bounds.Width - 10), (float) (bounds.Top + num4), format);
            }
        }

        public void DrawSeparator(Graphics g, Rectangle bounds)
        {
            int num = bounds.Y + (bounds.Height / 2);
            g.DrawLine(new Pen(SystemColors.ControlDark), (bounds.X + iconSize) + 7, num, (bounds.X + bounds.Width) - 2, num);
        }

        private void Initialize(Bitmap bitmap, Shortcut shortcut, EventHandler handler, System.Windows.Forms.ImageList list, int imageIndex)
        {
            base.OwnerDraw = true;
            base.Shortcut = shortcut;
            this.icon = bitmap;
            this.clickHandler = handler;
            this.imageList = list;
            this.imageIndex = imageIndex;
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            base.OnDrawItem(e);
            if (doColorUpdate)
            {
                this.DoUpdateMenuColors();
            }
            Graphics g = e.Graphics;
            Rectangle bounds = e.Bounds;
            bool selected = (e.State & DrawItemState.Selected) > DrawItemState.None;
            bool toplevel = base.Parent == base.Parent.GetMainMenu();
            bool hasicon = this.Icon != null;
            bool enabled = base.Enabled;
            if (toplevel)
            {
                this.DrawBackground(g, bounds, e.State, toplevel, hasicon, enabled);
                this.DrawMenuText(g, bounds, base.Text, this.shortcuttext, base.Enabled, toplevel, e.State);
            }
            else
            {
                this.DrawBackground(g, bounds, e.State, toplevel, hasicon, enabled);
                if (hasicon)
                {
                    this.DrawIcon(g, this.Icon, bounds, selected, base.Enabled, base.Checked);
                }
                else if ((this.imageList != null) && (this.imageIndex != -1))
                {
                    this.DrawIcon(g, this.imageList.Images[this.imageIndex], bounds, selected, base.Enabled, base.Checked);
                }
                else if (!(!base.Checked || hasicon))
                {
                    this.DrawMenuGlyph(g, bounds, selected, true);
                }
                else if (base.RadioCheck)
                {
                    this.DrawMenuGlyph(g, bounds, selected, false);
                }
                if (base.Text == "-")
                {
                    this.DrawSeparator(g, bounds);
                }
                else
                {
                    this.DrawMenuText(g, bounds, base.Text, this.shortcuttext, base.Enabled, toplevel, e.State);
                }
            }
        }

        protected override void OnMeasureItem(MeasureItemEventArgs e)
        {
            base.OnMeasureItem(e);
            if (base.Shortcut != Shortcut.None)
            {
                string str = "";
                int shortcut = (int) base.Shortcut;
                int num2 = shortcut & 0xff;
                if ((0x20000 & shortcut) > 0)
                {
                    str = str + "Ctrl+";
                }
                if ((0x10000 & shortcut) > 0)
                {
                    str = str + "Shift+";
                }
                if ((0x40000 & shortcut) > 0)
                {
                    str = str + "Alt+";
                }
                if ((num2 >= 0x70) && (num2 <= 0x7b))
                {
                    str = str + "F" + ((num2 - 0x70) + 1);
                }
                else if (base.Shortcut == Shortcut.Del)
                {
                    str = str + "Del";
                }
                else
                {
                    str = str + ((char) num2);
                }
                this.shortcuttext = str;
            }
            if (base.Text == "-")
            {
                e.ItemHeight = 8;
                e.ItemWidth = 4;
            }
            else
            {
                bool flag = base.Parent == base.Parent.GetMainMenu();
                string shortcuttext = this.shortcuttext;
                if (flag)
                {
                    shortcuttext = "";
                }
                int width = (int) e.Graphics.MeasureString(base.Text + shortcuttext, SystemInformation.MenuFont).Width;
                int num4 = 2;
                e.ItemHeight = SystemInformation.MenuHeight + num4;
                if (flag)
                {
                    e.ItemWidth = width - 5;
                }
                else
                {
                    e.ItemWidth = width + 0x2d;
                }
                itemHeight = e.ItemHeight;
            }
        }

        protected override void OnSelect(EventArgs e)
        {
            Menu parent = base.Parent;
            while (!(parent is CommandBarMenu) && (parent != null))
            {
                if (parent is MenuItemEx)
                {
                    parent = (parent as MenuItemEx).Parent;
                }
                else if (parent is MenuItem)
                {
                    parent = (parent as MenuItem).Parent;
                }
                else if (parent == base.Parent.GetMainMenu())
                {
                    parent = null;
                }
                else
                {
                    parent = null;
                }
            }
            if (parent is CommandBarMenu)
            {
                CommandBarMenu menu2 = (CommandBarMenu) parent;
                menu2.SelectedMenuItem = this;
            }
            base.OnSelect(e);
        }

        public static void UpdateMenuColors()
        {
            doColorUpdate = true;
        }

        public static void UpdateMenuColors(object sender, EventArgs e)
        {
            doColorUpdate = true;
        }

        public EventHandler ClickHandler
        {
            get
            {
                return this.clickHandler;
            }
            set
            {
                this.clickHandler = value;
            }
        }

        public Bitmap Icon
        {
            get
            {
                return this.icon;
            }
            set
            {
                this.icon = value;
            }
        }

        public int ImageIndex
        {
            get
            {
                return this.imageIndex;
            }
            set
            {
                this.imageIndex = value;
            }
        }

        public System.Windows.Forms.ImageList ImageList
        {
            get
            {
                return this.imageList;
            }
            set
            {
                this.imageList = value;
            }
        }

        public string ShortcutText
        {
            get
            {
                return this.shortcuttext;
            }
            set
            {
                this.shortcuttext = value;
            }
        }

        public Color TransparentColor
        {
            get
            {
                return transparentColor;
            }
            set
            {
                transparentColor = value;
            }
        }
    }
}

